//
//  discoverTitleView.m
//  Focus
//
//  Created by xwc on 14-10-31.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "discoverTitleView.h"
#import "UIImageView+WebCache.h"
#import "discoverTopCell.h"
#import "discoverImageCell.h"
#import "discoverSmallCell.h"
#import "webViewController.h"

@implementation discoverTitleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumInteritemSpacing = 1;
        flowLayout.minimumLineSpacing = 1;
        flowLayout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:flowLayout];
        [self addSubview:self.collectionView];
        [_collectionView release];
    }
    return self;
}

- (void)layoutSubviews
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = 0.1;
    flowLayout.minimumLineSpacing = 0.5;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.scrollEnabled = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[discoverTopCell class] forCellWithReuseIdentifier:@"discoverTopCell"];
    [self.collectionView registerClass:[discoverImageCell class] forCellWithReuseIdentifier:@"discoverImageCell"];
    [self.collectionView registerClass:[discoverSmallCell class] forCellWithReuseIdentifier:@"discoverSmallCell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 7;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        discoverTopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"discoverTopCell" forIndexPath:indexPath];
        cell.discoverModel = self.discoverModel;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else if (indexPath.item == 1) {
        discoverImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"discoverImageCell" forIndexPath:indexPath];
        cell.discoverModel = self.discoverModel;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else if (indexPath.item == 2) {
        discoverSmallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"discoverSmallCell" forIndexPath:indexPath];
        self.discoverModel.location = 2;
        cell.discoverModel = self.discoverModel;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else if (indexPath.item == 3) {
        discoverSmallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"discoverSmallCell" forIndexPath:indexPath];
        self.discoverModel.location = 3;
        cell.discoverModel = self.discoverModel;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else if (indexPath.item == 4) {
        self.discoverModel.location = 4;
        discoverSmallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"discoverSmallCell" forIndexPath:indexPath];
        cell.discoverModel = self.discoverModel;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else if (indexPath.item == 5) {
        discoverSmallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"discoverSmallCell" forIndexPath:indexPath];
        self.discoverModel.location = 5;
        cell.discoverModel = self.discoverModel;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else {
        discoverSmallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"discoverSmallCell" forIndexPath:indexPath];
        self.discoverModel.location = 6;
        cell.discoverModel = self.discoverModel;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item != 0) {
        NSString *url = [NSString string];
        NSString *pk = [NSString string];
        if (indexPath.item == 1) {
            url = self.discoverModel.weburl0;
            pk = self.discoverModel.pk0;
        } else if (indexPath.item == 2) {
            url = self.discoverModel.weburl1;
            pk = self.discoverModel.pk1;
        } else if (indexPath.item == 3) {
            url = self.discoverModel.weburl2;
            pk = self.discoverModel.pk2;
        } else if (indexPath.item == 4) {
            url = self.discoverModel.weburl3;
            pk = self.discoverModel.pk3;
        } else if (indexPath.item == 5) {
            url = self.discoverModel.weburl4;
            pk = self.discoverModel.pk4;
        } else if (indexPath.item == 6) {
            url = self.discoverModel.weburl5;
            pk = self.discoverModel.pk5;
        }
//        NSLog(@"*******%@",url);
        webViewController *webView = [[webViewController alloc] init];
        webView.url = url;
        webView.integer = 1;
        webView.pk = pk;
        webView.i = 1;
        [self.viewController presentViewController:webView animated:YES completion:^{
        }];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0 ) {
        return CGSizeMake(self.frame.size.width - 2, self.frame.size.height / 6);
    } else if (indexPath.item == 1) {
        return CGSizeMake(self.frame.size.width - 2, self.frame.size.height / 3);
    } else if (indexPath.item == 2) {
        return CGSizeMake(self.frame.size.width / 2 - 2, self.frame.size.height / 6);
    } else if (indexPath.item == 3) {
        return CGSizeMake(self.frame.size.width / 2 - 2, self.frame.size.height / 6);
    } else if (indexPath.item == 4) {
        return CGSizeMake(self.frame.size.width / 2 - 2, self.frame.size.height / 6);
    } else if (indexPath.item == 5) {
        return CGSizeMake(self.frame.size.width / 2 - 2, self.frame.size.height / 6);
    } else {
        return CGSizeMake(self.frame.size.width - 2, self.frame.size.height / 6);
    }
}

- (void)dealloc
{
    [_discoverModel release];
    [_collectionView release];
    [super dealloc];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
